package sample.client;

import sample.client.event.SelectClientEvent;
import sample.client.event.SetEnabledEvent;
import sample.shared.result.Client;

import com.google.gwt.event.shared.HandlerManager;
import com.google.inject.Inject;

public class CMModelImpl extends CMModelBase
{
  @Inject
  public CMModelImpl(HandlerManager eventBus) {
    this.eventBus = eventBus;
  }

  @Override
  public void onUpload() {}

  @Override
  public void onClientNameChanged() {
    Client client = getClient();
    boolean clientOK =
        client != null && client.getName().length() > 0
            && !client.getName().contains("...");
    setGotoEnabled(clientOK);
    setUploadEnabled(clientOK);
    setClientCodeText(clientOK ? client.getCode() : "N/A");
    eventBus.fireEvent(new SelectClientEvent(client));
  }

  @Override
  public void setGotoEnabled(boolean gotoEnabled) {
    super.setGotoEnabled(gotoEnabled);
    eventBus.fireEvent(new SetEnabledEvent(SetEnabledEvent.Option.GOTO,
        gotoEnabled));

  }

}
